package com.fz.controller;

import com.alibaba.fastjson.JSONObject;
import com.fz.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Z-C-Y on 2018-01-30.
 */
@Controller
public class TimerController {
   // @RequestMapping("/timer") @ResponseBody
    public String timer(){
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
         return sdf.format(new Date());
    }
    //@RequestMapping("/timer") @ResponseBody
    public void t(PrintWriter out){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        out.print(sdf.format(new Date()));
    }

    @RequestMapping("/timer")
    public void aa(HttpServletResponse resp) throws IOException {
      resp.setCharacterEncoding("utf-8");
      resp.setContentType("text/html;charset=utf-8");
      PrintWriter out = resp.getWriter();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      out.print(sdf.format(new Date()));
    //  out.print(sdf.format(new Date()));
      out.flush();
      out.close();
    }

    /*使用fastjson*/
    //@RequestMapping("/stu") @ResponseBody
    public String stu(){
        String[] addr = new String[]{"濮阳","上海"};
         return JSONObject.toJSONString(addr);
    }

    /*连接数据库*/
    @RequestMapping("/student") @ResponseBody
   public  String  student() {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = sf.openSession();
        StudentMapper sm = session.getMapper(StudentMapper.class);

        return JSONObject.toJSONString(sm.query());


    }

   }








